using Front._1117_2;
using MySqlConnector;

namespace Front._1118;

public partial class Login : Form
{
    public Login()
    {
        InitializeComponent();
    }

    private void Login_Load(object sender, EventArgs e)
    {
        this.textBox1.Text = "admin_super01";
        this.textBox2.Text = "123456";
    }//不用手动输入账号密码了

    private void button1_Click(object sender, EventArgs e)
    {
        // 测试数据库连接
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=qwert789;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        String sql = "SELECT * FROM admin_info where admin_account = @username and admin_password = @pwd";
        String username = this.textBox1.Text;
        String pwd = this.textBox2.Text;
        pwd = MD5Helper.GetMD5(pwd);
        using var command = new MySqlCommand(sql, connection);
        //给sql变量提供值
        command.Parameters.AddWithValue("@username", username);
        command.Parameters.AddWithValue("@pwd", pwd);
        using var reader = command.ExecuteReader();
        if (reader.HasRows)
        {
            MainFrom mainFrom = new MainFrom();
            mainFrom.Show();
            this.Hide();
        }
        else
        {
            MessageBox.Show("Invalid username or password");
            textBox1.Text = "";
            textBox1.Text = "";
        }
        
    }
}